package com.zucc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.zucc.controller.req.*;
import com.zucc.controller.resp.DictItemResp;
import com.zucc.controller.resp.DictResp;
import com.zucc.entity.Dict;
import com.zucc.entity.DictItem;
import com.zucc.entity.DictItemMapper;
import com.zucc.entity.DictMapper;
import com.zucc.entity.base.MyMapper;
import com.zucc.exception.SyzxException;
import com.zucc.service.DictService;
import com.zucc.service.base.MyServiceImpl;
import com.zucc.service.base.PageData;
import com.zucc.util.MyBeanUtil;
import com.zucc.util.MyPageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author : charspan
 * @Description :
 * @create : 2021-12-02 6:00 下午
 */
@Service
public class DictServiceImpl extends MyServiceImpl<Dict> implements DictService {
    @Resource
    private DictMapper dictMapper;

    @Resource
    private DictItemMapper dictItemMapper;

    @Override
    public MyMapper<Dict> mapper() {
        return dictMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(DictAddReq req) {
        Dict dict = new Dict();
        BeanUtil.copyProperties(req, dict);
        dict.setDelFlag(0);
        dict.setCreateTime(DateUtil.date());
        dict.setModifyTime(DateUtil.date());
        super.saveKey(dict);
    }

    @Override
    public PageData<DictResp> page(DictSearchReq req) {
        Example example = new Example(Dict.class);
        example.setOrderByClause("modify_time DESC");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("delFlag", 0);
        if (StrUtil.isNotBlank(req.getType())) {
            criteria.andLike("type", "%" + req.getType() + "%");
        }

        if (StrUtil.isNotBlank(req.getName())) {
            criteria.andLike("name", "%" + req.getName() + "%");
        }

        PageData<Dict> pageData = super.findByPage(req, example);
        PageData<DictResp> convert = MyPageUtil.convert(pageData, DictResp.class);
        return convert;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDictItem(DictItemAddReq req) {
        Dict dictExist = super.findById(req.getDictId());
        if (Objects.isNull(dictExist)) {
            throw new SyzxException("系统找不到相应编号的字典信息");
        }
        DictItem dictItem = new DictItem();
        BeanUtil.copyProperties(req, dictItem);
        dictItem.setDelFlag(0);
        dictItem.setCreateTime(DateUtil.date());
        dictItem.setModifyTime(DateUtil.date());
        dictItemMapper.insertUseGeneratedKeys(dictItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifyDictItem(DictItemModifyReq req) {
        DictItem dictItemExist = dictItemMapper.selectByPrimaryKey(req.getId());
        if (Objects.isNull(dictItemExist)) {
            throw new SyzxException("系统找不到相应编号的字典项信息");
        }
        BeanUtil.copyProperties(req, dictItemExist, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        dictItemExist.setModifyTime(DateUtil.date());
        dictItemMapper.updateByPrimaryKeySelective(dictItemExist);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOne(Long id) {
        DictItem dictItem = dictItemMapper.selectByPrimaryKey(id);
        if (Objects.isNull(dictItem)) {
            throw new SyzxException("系统找不到相应编号的字典项信息");
        }
        dictItem.setDelFlag(1);
        dictItem.setDeleteTime(DateUtil.date());
        dictItemMapper.updateByPrimaryKeySelective(dictItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMulti(Ids req) {
        req.getIds().forEach(this::deleteOne);
    }

    @Override
    public List<DictItemResp> getDictItemList(Long id) {
        Example exp = new Example(DictItem.class);
        exp.setOrderByClause("modify_time DESC");
        exp.createCriteria().andEqualTo("delFlag", 0)
                .andEqualTo("dictId", id);
        List<DictItem> dictItems = dictItemMapper.selectByExample(exp);
        return MyBeanUtil.converterList(dictItems, DictItemResp.class);
    }

}
